package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.ArticleAddDTO;
import org.jsola.hr.dto.ArticleUpdateDTO;
import org.jsola.hr.entity.ArticleDO;
import org.jsola.hr.query.ArticleQuery;
import org.jsola.hr.service.IArticleService;
import org.jsola.hr.vo.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * 人力文章
 *
 * @author zhr
 */
@Component
public class ArticleTestHelper extends BaseTestHelper {
    
    @Autowired
    private IArticleService articleService;

    /**
     * 生成add测试数据
     * @return ArticleAddDTO
     */
    public ArticleAddDTO getArticleAddDTO(){
        ArticleAddDTO articleAddDTO = new ArticleAddDTO();
        articleAddDTO.setContent(getRandomValue("longtext",2147483647));
        articleAddDTO.setIssue(SafeKit.getInteger(getRandomValue("int",1)));
        articleAddDTO.setIsTop(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        articleAddDTO.setArticleTitle(getRandomValue("varchar",30));
        articleAddDTO.setArticleTypeId(SafeKit.getLong(getRandomValue("bigint",11)));
        articleAddDTO.setReleaseTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        articleAddDTO.setBrowseNum(SafeKit.getInteger(getRandomValue("int",11)));
        articleAddDTO.setMainPictureUrl(getRandomValue("varchar",255));
        return articleAddDTO;
    }


    /**
     * 生成update测试数据
     * @return ArticleUpdateDTO
     */
    public ArticleUpdateDTO getArticleUpdateDTO(ArticleVO articleVO){
        return articleService.selectDOById(articleVO.getId(), SITE_ID).to(ArticleUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return ArticleQuery
     */
    public ArticleQuery getArticleQuery() {
        ArticleQuery articleQuery = new ArticleQuery();
        List<ArticleDO> articleDOList = articleService.list(new ArticleDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(articleDOList)) {
            ArticleDO articleDO = articleDOList.get(0);
            articleQuery = articleDO.to(ArticleQuery.class);
        }
        return articleQuery;
    }

    /**
     * 保存示例
     * @return ArticleVO
     */
    public ArticleVO saveArticleExample(){
        ArticleAddDTO addDTO = this.getArticleAddDTO();
        ArticleVO articleVO = articleService.save(addDTO,buildTokenUser());
        return articleVO;
    }


}

